package com.pl.service.impl;

import com.alibaba.excel.EasyExcel;
import com.pl.entity.Order;
import com.pl.dao.OrderDao;
import com.pl.entity.SysUser;
import com.pl.lister.OrderLister;
import com.pl.lister.SysUserReadLister;
import com.pl.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2024-04-30 15:17:26
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    /**
     * 通过ID查询单条数据
     *
     * @param oid 主键
     * @return 实例对象
     */
    @Override
    public Order queryById(Integer oid) {
        return this.orderDao.queryById(oid);
    }


    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<Order> queryByPage(Map<String, Object> map) {
        return orderDao.queryAllByLimit(map);
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order insert(Order order) {
        this.orderDao.insert(order);
        return order;
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order update(Order order) {
        this.orderDao.update(order);
        return this.queryById(order.getOid());
    }

    /**
     * 通过主键删除数据
     *
     * @param oid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer oid) {
        return this.orderDao.deleteById(oid) > 0;
    }

    /**
     * 批量删除
     * @param list 参数
     * @return
     */
    @Override
    public boolean deleteBatch(List<Integer> b) {
        int i = orderDao.deleteBatch(b);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean finAll() {
        List<Order> orders = orderDao.finAll();
        if (orders != null) {
            return true;
        }
        return false;
    }
//批量插入配合监听器使用
    @Override
    public boolean inserts(List<Order> orderList) {
        int num = orderDao.insertBatch(orderList);
        if (num > 0) {
            return true;
        }
        return false;
    }
    /**
     * 导入文件
     *
     * @param file
     * @return
     */
    @Override
    public String importOrderFile(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), Order.class, new OrderLister(this)).sheet("后勤订单信息").doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }

    /**
     * 导出文件
     *
     * @param response
     */
    @Override
    public void exportOrderFile(HttpServletResponse response) {
        List<Order> orderList = orderDao.finAll();
        try {
            EasyExcel.write(response.getOutputStream(), Order.class).sheet("后勤订单信息").doWrite(orderList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
